package com.zhhtech.zhh.ui.helper

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import com.zhhtech.zhh.ui.ext.DefaultLifecycleObserver

typealias ActivityForResult = (ActivityResult) -> Unit

typealias IMsa = IManageStartActivity

fun msa(): ManageStartActivity = ManageStartActivity()

interface IManageStartActivity {

    val mContext: Context?

    /**
     * 初始化
     */
    fun <Host : LifecycleOwner> Host.initManageStartActivity()

    fun launchStartActivityForResult(
        clazz: Class<*>,
        isNeedResultOk: Boolean = true,
        options: (Bundle.() -> Unit)? = null,
        activityForResult: ActivityForResult? = null,
    )

    fun launchStartActivityForResult(
        intent: Intent,
        isNeedResultOk: Boolean = true,
        activityForResult: ActivityForResult? = null,
    )
}

class ManageStartActivity : IManageStartActivity {
    private var msaContext: Context? = null
    private var mActivityForResult: ActivityForResult? = null
    private var mIsNeedResultOk: Boolean? = null
    private var mStartActivityForResultLauncher: ActivityResultLauncher<Intent>? = null
    private val activityResultCallback = ActivityResultCallback<ActivityResult> { activityResult ->
        if (mIsNeedResultOk == true) {
            if (activityResult.resultCode == Activity.RESULT_OK) {
                mActivityForResult?.invoke(activityResult)
            }
        } else {
            mActivityForResult?.invoke(activityResult)
        }
    }
    override val mContext: Context? get() = msaContext

    override fun <Host : LifecycleOwner> Host.initManageStartActivity() {
        mStartActivityForResultLauncher = when (this) {
            is ComponentActivity -> {
                msaContext = this
                registerForActivityResult(ActivityResultContracts.StartActivityForResult(), activityResultCallback)
            }
            is Fragment -> {
                msaContext = requireContext()
                registerForActivityResult(ActivityResultContracts.StartActivityForResult(), activityResultCallback)
            }
            else -> {
                throw IllegalArgumentException("IManageStartActivity only support `ComponentActivity` and `Fragment`.")
            }
        }
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onDestroy(owner: LifecycleOwner) {
                msaContext = null
                mActivityForResult = null
                mIsNeedResultOk = null
            }
        })
    }

    override fun launchStartActivityForResult(
        clazz: Class<*>,
        isNeedResultOk: Boolean,
        options: (Bundle.() -> Unit)?,
        activityForResult: ActivityForResult?,
    ) {
        val intent = Intent(msaContext, clazz)
        options?.let { intent.putExtras(Bundle().also(it)) }
        launchStartActivityForResult(intent, isNeedResultOk, activityForResult)
    }

    override fun launchStartActivityForResult(intent: Intent, isNeedResultOk: Boolean, activityForResult: ActivityForResult?) {
        mActivityForResult = activityForResult
        mIsNeedResultOk = isNeedResultOk
        mStartActivityForResultLauncher?.launch(intent)
    }

}